layui.use(['table', 'jquery','element','layer','form'], function () {
    var table = layui.table;
    var $ = layui.jquery;
    var element = layui.element;
    var layer = layui.layer;
    var form = layui.form;
    initTree(); // 初始化树
    // 刷新树形结构的内容
    function initTree(){
        console.log(123);
        var setting = {
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pid",
                    rootPId: 0
                }
            },
            view: {
                showIcon: false,
                addHoverDom: addHoverDom,
                removeHoverDom: removeHoverDom,
            },
            callback : {
                beforeClick : validStatus//这里
            }
        };

        // 禁止点击节点
        function validStatus(){
            return false;
        }

        var  zNodes = null;

        // 获取数据
        $.ajax({
            url : ctx+"/eng/stock/list",
            async : false,
            success : function(res){
                zNodes = res.data;
            }
        });
        $.fn.zTree.init($("#tree"), setting, zNodes);
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        treeObj.expandAll(true);
    }


    function addHoverDom(treeId, treeNode) {
        $('.curSelectedNode').removeClass('curSelectedNode')
        // 获取节点的内容
        var sObj = $("#" + treeNode.tId + "_span");
        var showPrice = '';
        // 展示浮动价格
        if(!treeNode.children){
            showPrice += "   <span>库存: "+treeNode.stock+"</span>";
            // 追加盘点操作
            showPrice +="<span class='layui-icon layui-icon-fonts-clear stock_cal'  title='盘点' info='"+JSON.stringify(treeNode)+"'></span>";

            // 追加库存明细
            showPrice +="<span class='layui-icon layui-icon-search stock_detail'  title='库存进出明细' info='"+JSON.stringify(treeNode)+"'></span>";
        }

        // 判断一下
        if(sObj.next().length == 0){
            sObj.after(showPrice);
        }
    };
    function removeHoverDom(treeId, treeNode) {
        // 删除掉后面的span
        var sObj = $("#" + treeNode.tId + "_span");
        sObj.next().remove();
        sObj.next().remove();
        sObj.next().remove();
        sObj.next().remove();
        sObj.next().remove();
    };

    // 盘点操作
    $('#tree').on('click','.stock_cal',function(e){
        ZtreeNode = JSON.parse($(e.target).attr('info')) ;

        // 输入真实的数量
        layer.prompt({
            title : '输入现存值',
            offset :'100px',
        },function(value, index, elem){
            $.ajax({
                url : ctx+"/eng/stock/stockCal",
                data:{
                    typeId : ZtreeNode.id,
                    num : value
                },
                async : false,
                type : 'post',
                success : function(res){
                    if(res.success){
                        layer.msg(res.msg,{icon:1});
                        layer.close(index);
                        initTree();
                    }else{
                        layer.msg(res.msg,{icon:2}); layer.msg(res.msg,{icon:1});
                    }
                }
            });
        });
    });


    // 库存进出明细
    $('#tree').on('click','.stock_detail',function(e){
        var ZtreeNode = JSON.parse($(e.target).attr('info')) ;
        window.open(ctx+"/eng/stock/stockDeatil?typeId="+ZtreeNode.id);

    });

});